﻿using System;
using System.Collections;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml.Linq;
using BusinessObjects.BO_GetObjects;
using DigiLib;

public partial class controls_admin_Modules : Admin_Controls_Base {
    private int location_id;
    protected void Page_Load(object sender, EventArgs e) {
        location_id = base.getRequest("location_id", 0);
        base.removeHGC(divMessage);
        if (!IsPostBack) {
            try {
                base.bindDDL(ddlLocation, BO_portal_locations.SelectAll(1), "LOCATION_NAME", "LOCATION_ID");
                loadCategories();
                //ddlCategories.SelectedValue = Category_ID.ToString();
                //ddlLocation.SelectedValue = location_id.ToString();
                loadModule();
            } catch (Exception ex) { divMessage.InnerHtml = MessageBox.Error("Lỗi", "Phát sinh lỗi như sau: " + ex.Message); }
        }
    }
    protected void loadModule() {
        try {
            string keyword = string.Empty;
            if (!string.IsNullOrEmpty(txtSearch.Value) && !txtSearch.Value.Equals("Nhập tên module !"))
                keyword = txtSearch.Value.Trim();
            ObjDT = BO_portal_modules.SelectAdmin(Convert.ToInt32(ddlCategories.SelectedValue), Convert.ToInt32(ddlLocation.SelectedValue), keyword);
            base.bindRepeater(rptModule, ObjDT, divMessage);
        } catch (Exception ex) {
            divMessage.InnerHtml = MessageBox.Error("Lỗi", "Phát sinh lỗi như sau: " + ex.Message);
        }
    }
    private void loadCategories() {
        ddlCategories.Items.Clear();
        base.bindDDL(ddlCategories, BO_portal_categories.SelectAll(), "FULL_CATEGORY_NAME", "CATEGORY_ID");
    }
    protected void ddlCategories_SelectedIndexChanged(object sender, EventArgs e) {
        loadModule();
    }
    protected void btnSearch_Click(object sender, EventArgs e) {
        loadModule();
    }
    protected void ddlAction_SelectedIndexChanged(object sender, EventArgs e) {
        try {
            if (ddlAction.SelectedValue.Equals("0"))
                return;
            string selectedItems = GetSelectedItems(rptModule, "chkBox");
            if (String.IsNullOrEmpty(selectedItems)) {
                Javascripts.Alert(Page, "Phải chọn ít nhất 1 module!");
                return;
            }
            if (BO_portal_modules.Delete(selectedItems, ";", Convert.ToInt32(ddlAction.SelectedValue))) {
                divMessage.InnerHtml = MessageBox.Success("Thông Báo", "Thực hiện thành công.");
            } else {
                divMessage.InnerHtml = MessageBox.Error("Lỗi", "Chưa thực hiện được !");
            }
            loadModule();
        } catch (Exception ex) {
            divMessage.InnerHtml = MessageBox.Error("Lỗi hệ thống", "Phát hiện lỗi sau: " + ex.Message);
        }
    }
    protected void ddlLocation_SelectedIndexChanged(object sender, EventArgs e) {
        loadModule();
    }
}
